<template>
  <div>
    <slot></slot>
    <input :type="type" :name="name" :placeholder="message" autocomplete="off" @blur="blurs" @focus="inputFocus" @input="inputChange">
  </div>
</template>
<script>
  import bus from "../bus.js"
  export default {
    data() {
      return {
        message: this.placeholder
      }
    },
    props: {
      name: {
        type: String
      },
      placeholder: {
        type: String,
        default: ""
      },
      type: {
        type: String,
        default: "text"
      },
      pattern: {}
    },
    methods: {
      blurs(e) {
        var value = e.target.value
        var reg = new RegExp(this.pattern)
        if (!value) {
          e.target.className = 'error'
        }

        if (!reg.test(value)) {
          e.target.className = 'error'
        }
      },
      inputFocus(e) {
        var value = e.target.value
        this.message = this.placeholder

      },
      inputChange(e) {
        

        var value = e.target.value

        this.$emit('input',value)
        bus.$emit('changes')
        var reg = new RegExp(this.pattern)
        if (reg.test(value)) {
          e.target.className = ''
        }
      }



    }
  }

</script>
<style scoped>
  input {
    border: none;
    font-size: .4rem;
    padding-left: 10px;
  }
  
  .error {
    color: red;
  }
  
  .error::-webkit-input-placeholder {
    color: red;
  }
  
  .error::-moz-placeholder {
    color: red;
  }
  
  .error::-ms-input-placeholdr {
    color: red;
  }

</style>
